package jp.anago_dev.takarush_helper.ui;

import java.util.ArrayList;
import java.util.Calendar;

import jp.anago_dev.takarush_helper.database.DatabaseHelper;
import jp.anago_dev.takarush_helper.database.DatabaseParameter.LocationColumn;
import jp.anago_dev.takarush_helper.database.LocationDatabaseHandler;
import jp.anago_dev.takarush_helper.ui.actionbar.ActionBarActivity;

import org.apache.http.impl.cookie.DateUtils;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.content.ModernAsyncTask;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * 位置情報の履歴を取得します。
 * 
 * @author anago_dev
 * 
 */
public class LocationHistoryActivity extends ActionBarActivity {

    private ArrayList<String> mLocationHistories = new ArrayList<String>();

    private ArrayListFragment mArrayFragment;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create the list fragment and add it as our sole content.
        if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
            mArrayFragment = new ArrayListFragment();
        }

        new AsyncQueryTask().execute(null, null, null);
    }

    private void test() {
        getSupportFragmentManager().beginTransaction().add(android.R.id.content, mArrayFragment).commit();
    }

    private class ArrayListFragment extends ListFragment {

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mLocationHistories.toArray(new String[mLocationHistories.size()])));
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            System.out.println("Item clicked: " + id);
        }
    }

    /**
     * 非同期で位置情報をデータベースからロードします。
     */
    private class AsyncQueryTask extends ModernAsyncTask<Void, Void, Cursor> {

        @Override
        protected Cursor doInBackground(final Void... arg0) {

            // DBから読み込み
            return LocationDatabaseHandler.query(DatabaseHelper.getInstance(getApplicationContext()).getReadableDatabase());
        }

        @Override
        protected void onPostExecute(final Cursor cursor) {
            super.onPostExecute(cursor);

            // カーソルからデータ取り出し
            if (cursor != null && cursor.moveToFirst()) {
                do {
                    String lat = cursor.getString(cursor.getColumnIndex(LocationColumn.LATITUDE));
                    String lon = cursor.getString(cursor.getColumnIndex(LocationColumn.LONGITUDE));
                    final Calendar cal = Calendar.getInstance();
                    cal.setTimeInMillis(Long.parseLong(cursor.getString(cursor.getColumnIndex(LocationColumn.TIME))));
                    mLocationHistories.add("(" + lon + " , " + lat + ")" + DateUtils.formatDate(cal.getTime(), DateUtils.PATTERN_RFC1036));

                } while (cursor.moveToNext());
                cursor.close();
                test();
            }
        }

    }

}
